﻿using System;
using System.IO;
using System.Collections;
using UnityEngine;

public sealed class ResourceLoader : Singleton<ResourceLoader>
{
    public bool UseAssetBundle = true;

    public void Initialize()
    {
        if (UseAssetBundle)
        {
            AssetBundleManager.Instance.Initialize();
        }
    }

    protected override void OnRelease()
    {
        StopAllCoroutines();
        UnloadAllBundles();
    }

    public UnityEngine.Object LoadObject(string resourcePath)
    {
        return UseAssetBundle ? LoadFromBundle("Resources/" + resourcePath) : LoadFromResources(resourcePath);
    }

    public GameObject LoadGameObject(string resourcePath, bool bActive = true)
    {
        GameObject go = UseAssetBundle ? LoadFromBundle<GameObject>("Resources/" + resourcePath) : LoadFromResources<GameObject>(resourcePath);
        if (go != null)
        {
            go.SetActive(bActive);
        }
        return go;
    }

    public UnityEngine.Object LoadFromBundle(string assetPath)
    {
        return AssetBundleManager.Instance.LoadObjectFromAssetBundle<UnityEngine.Object>(assetPath);
    }

    public T LoadFromBundle<T>(string assetPath) where T : UnityEngine.Object
    {
        UnityEngine.Object obj = LoadFromBundle(assetPath);
        return SafeConvertObject<T>(obj);
    }

    public void LoadGameObjectAsync(string resourcePath, System.Action<GameObject> action)
    {
        if (string.IsNullOrEmpty(resourcePath))
        {
            return;
        }

        AssetBundleManager.Instance.LoadObjectFromAssetBundleAsync("Resources/" + resourcePath, action);
    }

    public UnityEngine.Object LoadFromResources(string resourcePath)
    {
        UnityEngine.Object obj = null;
        try
        {
            obj = Resources.Load(resourcePath);
        }
        catch (Exception ex)
        {
            Debug.LogError("ResourceService.LoadFromResources Error:" + ex.ToString());
        }
        return obj;
    }

    public T LoadFromResources<T>(string Path) where T : UnityEngine.Object
    {
        try
        {
            T go = Resources.Load<T>(Path);
            return go;
        }
        catch (Exception ex)
        {
            Debug.LogError("ResourceService.LoadFromResources<T> Error:" + ex.ToString());
        }
        return null;
    }

    public void Unload(UnityEngine.Object obj)
    {
        Resources.UnloadAsset(obj);
    }

    public void UnloadAllBundles()
    {
        //if (AssetBundleManager.isValid())
        {
            AssetBundleManager.Instance.UnloadDependencies();
        }
    }

    public void UnloadUnusedAssets()
    {
        Resources.UnloadUnusedAssets();
    }

    private T SafeConvertObject<T>(UnityEngine.Object obj) where T : UnityEngine.Object
    {
        T t = null;
        if (obj != null)
        {
            t = obj as T;
            if (t == null)
            {
                UnityEngine.Object.Destroy(obj);
                Debug.LogError("SafeConvertObject<T>(), type convert fail.");
            }
        }
        return t;
    }
}